﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MediaGarden.Services;
using Orchard;
using Orchard.Core.Routable.Models;
using Orchard.ContentManagement;
using MediaGarden.Models;
using MediaGarden.Pipeline;
using System.Drawing;
using Downplay.Mechanics.Plumbing.Models;

namespace MediaGarden.Images.Filters
{
    public class ImageSourceFilter : IMediaSourceFilter
    {
        public IOrchardServices _services { get; set; }

        public ImageSourceFilter(IOrchardServices services)
        {
            _services = services;
        }

        public IEnumerable<string> SupportedFormats()
        {
            return new[]{
                "Jpeg",
                "Png",
                // TODO: Gifs and Icos need careful handling due to complexities of the formats; multiple image parts, colour schemes, etc. They probably need their own filters to
                // supplement the processing here
                "Gif",
                "Ico",
                // TODO: There could be a distinction that the following two formats can't be displayed directly as external file. But maybe that should just be handled by Viewers,
                // so a Flash viewer would typically be fired up for non-web image formats
                "Bmp",
                "Tiff",
            };
        }

        public void BuildSourceMetadata(MediaQueryContext query, MediaLocationContext location, MediaSourceContext source)
        {
            var stream = location.Stream.GetStream();
            var bitmap = Image.FromStream(stream);
            source.Source.Metadata["SizeX"] = bitmap.Width.ToString();
            source.Source.Metadata["SizeY"] = bitmap.Height.ToString();
        }
 
        public void SourceFiltering(MediaContentContext filterContext, IMediaSource data)
        {
            // TODO: Chain thumbnail, watermark creation, etc.
        }
        public void SourceFiltered(MediaContentContext filterContext, IMediaSource data)
        {
        }

   }
}